package com.kiss.rpc;

import com.kiss.rpc.discovery.Registry;
import com.kiss.rpc.proxy.handler.RpcConsumerInvocationHandler;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/*
    根据传入的接口类、注册中心及组别，得到这个类的动态代理类，从而实现对每个方法的包装，使其可以
    1. 将调用封装为报文发送
    2. 等待回调结果
*/
@Data
public class ReferenceConfig<T> {

    private Class<T> interfaceRef;

    private Registry registry;

    private String group;

    public T get() {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        Class<T>[] classes = new Class[]{interfaceRef};
        InvocationHandler handler = new RpcConsumerInvocationHandler(registry, interfaceRef, group);

        Object helloProxy = Proxy.newProxyInstance(classLoader, classes, handler);

        return (T) helloProxy;
    }



}
